﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plugins
{
    public partial class UCPlugin : UserControl
    {
        //全部显示格式
        public PluginObject PluginObject { get; set; }
        private int count;
        public Guid Id { get; set; }
        public Action<int, string, string, string> refreshPluginIntroduction;
        private bool hovering = false;
        public UCPlugin(PluginObject pluginObject,int height,Action<int,string, string,string> refreshPluginIntroduction)
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.CacheText | ControlStyles.OptimizedDoubleBuffer, true);
            this.Id = Guid.NewGuid();
            this.Height = height;
            labName.Text = pluginObject.Name;
            PluginObject = pluginObject;
            this.refreshPluginIntroduction = refreshPluginIntroduction;
        }

        private void UCPlugin_Load(object sender, EventArgs e)
        {
        }

        private void UCPlugin_Paint(object sender, PaintEventArgs e)
        {
            //layout
            this.Width = this.Parent.Width;
            labName.Location = new Point(this.Width/2-labName.Width/2,this.Height/2-labName.Height/2);

            //drawing
            Graphics g = e.Graphics;
            g.DrawLine(Pens.LightGray, new Point(0, this.Height-1), new Point(this.Width, this.Height-1));

            //other
            count = PluginManager.RunningPlugins.Where(s => s.PluginId == Id).Count();
            this.BackColor =hovering? Color.FromArgb(255, 128, 0) : count > 0 ? Color.FromArgb(28,74,144) : Color.FromArgb(18,79,38);
        }
        
        private void UCPlugin_DoubleClick(object sender, EventArgs e)
        {
            PluginManager.RunPlugin(PluginObject,Id);
            refreshPluginIntroduction(count + 1, PluginObject.Name, PluginObject.Version, PluginObject.Description);
        }
        

        private void UCPlugin_MouseEnter(object sender, EventArgs e)
        {
            hovering = true;
            Invalidate();
            refreshPluginIntroduction(count, PluginObject.Name, PluginObject.Version, PluginObject.Description);
        }
        private void UCPlugin_MouseLeave(object sender, EventArgs e)
        {
            hovering = false;
            Invalidate();
        }
    }
}
